package com.example.thridhero.sharedpreferencestest;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button saveDataBtn;

    private Button saveDataWayBtn;

    private Button saveDataWay2;

    private String TAG = "";

    private Button restoreData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        saveDataBtn = (Button) findViewById(R.id.save_data);
        saveDataWayBtn = (Button) findViewById(R.id.save_data_way);
        saveDataWay2 = (Button) findViewById(R.id.save_data_way2);
        restoreData = (Button) findViewById(R.id.restore_data);

        saveDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences =    PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                SharedPreferences.Editor editor =  sharedPreferences.edit();
                editor.putString("name","Tom");
                editor.putInt("age",28);
                editor.putBoolean("married",false);
                editor.apply();
            }
        });

        saveDataWayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences  sharedPreferences =  getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor editor2 =  sharedPreferences.edit();
                editor2.putString("name","cherry");
                editor2.putBoolean("married",true);
                editor2.putInt("age",20);
                editor2.apply();
            }
        });

        saveDataWay2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences  sharedPreferences =  getPreferences(MODE_APPEND);
                SharedPreferences.Editor editor3 = sharedPreferences.edit();
                editor3.putBoolean("married",false);
                editor3.putInt("age",30);
                editor3.putString("name","Dennis");
                editor3.apply();
            }
        });
        restoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
                String name = pref.getString("name","no name");
                int age = pref.getInt("age",0);
                Boolean married = pref.getBoolean("married",false);
                String Str = "姓名是："+name+"年龄"+age+"是否已婚"+married;
                Toast.makeText(MainActivity.this,Str,Toast.LENGTH_SHORT).show();
            }
        });


    }
}
